<template>
	<view>
		<view class="content">
			<image src="../../static/icon1.png" mode=""></image>
		</view>
		
		<button class="textcentent" style="color: rgba(36,188,129,1);margin-top: 120rpx;" open-type="getUserInfo"
			lang="zh_CN" @getuserinfo="bindGetUserInfo"> 点击授权 </button>
	</view>
</template>

<script>
	import {mapMutations} from 'vuex'
	export default {
		data() {
			return {
				// appid:'wx053c976be83edb57',
				appid:'wx8ab166cb6a27da41',
				select:'b3a3ef2d8790cfa2207e12d20043b912',
				code: '',
				// 微信openid
				openid: '',
				// 微信昵称
				nickname: '',
				// 	微信头像
				avatar: '',
				//身份验证token值
				token: '',
			}
		},
		onLoad(){ 
			uni.getSetting({
			   success(res) {
				  if(res.authSetting){
					  console.log('登录',res.authSetting)
				  }else{
					  console.log('未登录')
				  }
			   }
			})
		},
		methods: {
			// 微信授权的点击事件 
			bindGetUserInfo(e) {
				let obj ={}
				let userinfo = e.detail.userInfo
				// console.log(userinfo)
				if (userinfo) {
					uni.login({
						provider: 'weixin',
						success: res => {
							this.code = res.code
							// console.log('code'+this.code)
							uni.request({
								url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + this
									.appid + '&secret=' + this.select + '&js_code=' + this.code +
									'&grant_type=authorization_code',
								success: res => {
									this.nickname = userinfo.nickName
									this.avatar = userinfo.avatarUrl
									this.openid = res.data.openid
									// console.log('openid'+this.openid)
									uni.request({
										url: 'http://test.hanshupai.com/api/login',
										method: "POST",
										data: {
											nickname: this.nickname,
											avatar: this.avatar,
											openid: this.openid,
											// mobile: '1346157465',
											// longitude: '65',
											// latitude: '654'
										},
										success: res => {
											obj.avatar = res.data.data.avatar
											obj.token = res.data.data.token
											obj.nickname = res.data.data.nickname
											this.$store.commit('Userinfo',obj);
											uni.switchTab({
												url: '../index/index'
											});
										}
									})
								}
							})
						},
					});
				} else {
					uni.showModal({
						title: '提示',
						showCancel: false,
						content: '拒绝登录后，不能浏览更多信息',
						success: function(res) {
							if (res.confirm) {
								
							} else if (res.cancel) {
								
							}
						}
					})
				}
			}
		},
	}
</script>

<style>
	.content {
		width: 684rpx;
		height: 514rpx;
		margin: 44rpx auto;
	}

	.content>image {
		width: 100%;
	}

	.textcentent {
		width: 392rpx;
		height: 86rpx;
		margin: 74rpx auto;
		text-align: center;
		line-height: 86rpx;
		border-radius: 43rpx;
	}
</style>
